home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgzip_022-src / macos / Posix / ThinkCPosix Sources / dup2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  595 b   |  26 lines  |  [TEXT/MPS ]

  1. /* $Id: $ */
  2.  
  3. /*
  4.  * This implementation of dup2() disables oldfd;`
  5.  * in effect, it assumes that oldfd is closed
  6.  * immediately after dup2() (as is almost always the case).
  7.  * The problem is that if __file[oldfd]->func() is left unchanged
  8.  * then close(oldfd) will also close newfd.
  9.  */
  10.  
  11. #include "ThinkCPosix.h"
  12.  
  13. int dup2(int oldfd, int newfd)
  14. {
  15.     FILE *oldfp = fdopen(oldfd, "");
  16.  
  17.     if (oldfp == NULL || (unsigned)newfd >= FOPEN_MAX) {
  18.         errno = EBADF;
  19.         return -1;
  20.     }
  21.     close(newfd);
  22.     memcpy(&__file[newfd], (char*)oldfp, sizeof(FILE));
  23.     memset((char*)oldfp, 0, sizeof(FILE));
  24.     return newfd;
  25. }
  26.